package com.yuanlai.quartz.factory;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

/**
 * 创建Job的工厂
 * @author yuanlai
 *
 */
@Component
public class JobFactory extends AdaptableJobFactory{
	@Autowired
	private AutowireCapableBeanFactory capableBeanFactory;

	 /**
	  * AdaptableJobFactory是quartz提供的工厂，用来生成Job实例
	  */
	@Override
	protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
		// TODO Auto-generated method stub
		 Object jobInstance = super.createJobInstance(bundle);
		 //spring对Job进行依赖注入(如果Job需要注入service层来执行代码)
		 capableBeanFactory.autowireBean(jobInstance);
		return jobInstance;
	}

	
	
}
